package com.biz.primus.ms.evaluation.service.helper;

import com.biz.primus.model.order.vo.OrderVO;
import com.biz.primus.model.order.vo.request.OrderCodeVO;
import com.biz.primus.model.order.vo.response.OrderResponseVO;
import com.biz.primus.ms.base.service.AbstractBaseService;
import com.biz.primus.ms.evaluation.dao.po.OrderEvaluation;
import com.biz.primus.ms.evaluation.dao.po.ProductEvaluation;
import com.biz.primus.ms.evaluation.dao.repository.OrderEvaluationRepository;
import com.biz.primus.ms.evaluation.feign.OrderFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 评价服务帮助
 *
 * @author Tiger Feng
 * @date 2017年11月06日
 */
@Slf4j
@Service
public class EvaluationServiceHelper extends AbstractBaseService {
    @Autowired
    private OrderEvaluationRepository orderEvaluationRepository;
    @Autowired
    private OrderFeignClient orderFeignClient;

    /**
     * 持久化订单评价（包括物流评价、商品评价）
     * @param orderEvaluation
     * @return
     */
    public OrderEvaluation saveOrderEvaluation(OrderEvaluation orderEvaluation){
        // 为订单评价设置ID
        orderEvaluation.setId(idService.getNextId());
        // 为物流评价设置ID
        if(orderEvaluation.getDeliveryEvaluation() != null){
            orderEvaluation.getDeliveryEvaluation().setId(idService.getNextId());
        }
        // 为商品评价设置ID
        if(orderEvaluation.getProductEvaluations() != null && !orderEvaluation.getProductEvaluations().isEmpty()){
            for(ProductEvaluation productEvaluation : orderEvaluation.getProductEvaluations()){
                productEvaluation.setId(idService.getNextId());
            }
        }
        // 调用dao持久化订单评价
        return orderEvaluationRepository.save(orderEvaluation);
    }

    /**
     * 根据订单编号获取订单评价
     * @param orderCode
     * @return
     */
    public OrderEvaluation getOrderEvaluationByOrderCode(String orderCode) {
        return orderEvaluationRepository.findByOrderCode(orderCode);
    }

    /**
     * 根据订单编号获取订单信息
     * @param orderCode
     * @return
     */
    public OrderResponseVO getOrderVOByOrderCode(String orderCode) {
        OrderCodeVO orderCodeVO = new OrderCodeVO();
        orderCodeVO.setOrderCode(orderCode);
        return orderFeignClient.queryByOrderCode(orderCodeVO);
    }

    /**
     * 调用订单接口更新订单状态为已经评价
     * @param orderCode
     */
    public void evaluateOrderState(String orderCode){
        orderFeignClient.evaluateOrderState(orderCode);
    }
}
